//
//  RectView.m
//  aa
//
//  Created by 刘东旭 on 2017/8/11.
//  Copyright © 2017年 meicam. All rights reserved.
//

#import "RectView.h"
#import "Utils.h"


@interface RectView ()

@property (strong, nonatomic) UIButton *leftTop;
//@property (strong, nonatomic) UIButton *rightTop;
//@property (strong, nonatomic) UIButton *rightBottom;
//@property (strong, nonatomic) UIButton *leftBottom;
@property (assign, nonatomic) CGPoint leftTopPoint;
@property (assign, nonatomic) CGPoint rightTopPoint;
@property (assign, nonatomic) CGPoint rightBottompPoint;
@property (assign, nonatomic) CGPoint leftBottomPoint;
@property (assign, nonatomic) BOOL isDown;

@end

@implementation RectView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.leftTop];
    }
    return self;
}

//MARK Getter
- (UIButton *)leftTop {
    if (!_leftTop) {
        _leftTop = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        [_leftTop setImage:[UIImage imageFromBundle:@"rect_DEL"] forState:UIControlStateNormal];
        [_leftTop addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftTop;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    
    //有几个手指触摸屏幕，多个手指不执行旋转
    NSUInteger toucheNum = [[event allTouches] count];
    if ( toucheNum > 1 ) {
        return;
    }
    
    //self.tranformView，你想旋转的视图
    if (![touch.view isEqual:self]) {
        return;
    }
    
    CGPoint currentPoint = [touch locationInView:touch.view];//当前手指的坐标
    
    if ([self.delegate respondsToSelector:@selector(rectView:touchBeganPoint:)]) {
        [self.delegate rectView:self touchBeganPoint:currentPoint];
    }
    if (currentPoint.x > (self.rightBottompPoint.x-13) && currentPoint.x < (self.rightBottompPoint.x+13) && currentPoint.y > (self.rightBottompPoint.y-13) && currentPoint.y < (self.rightBottompPoint.y+13)) {
        self.isDown = true;
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.isDown = false;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    
    NSUInteger toucheNum = [[event allTouches] count];//有几个手指触摸屏幕
    if ( toucheNum > 1 ) {
        return;//多个手指不执行旋转
    }
    
    //self.tranformView，你想旋转的视图
    if (![touch.view isEqual:self]) {
        return;
    }
    
    CGPoint center = [self getCenter];
    CGPoint currentPoint = [touch locationInView:touch.view];//当前手指的坐标
    CGPoint previousPoint = [touch previousLocationInView:touch.view];//上一个坐标
    
    
    float x = currentPoint.x-previousPoint.x;
    float y = currentPoint.y-previousPoint.y;
    NSInteger s = 10;//跟边界的距离
    
    if (self.videoscale == NvSSVideoCreateScaleHorizontal) {
        //向左滑
        if (x<0) {
            if (self.leftTopPoint.x <= s) {
                return;
            }
        } else {//向右滑
            if (self.rightBottompPoint.x >= SCREEN_WIDTH-s) {
                return;
            }
        }
        //向上滑
        if (y<0) {
            if (self.leftTopPoint.y <= s) {
                return;
            }
        } else {//向下滑
            if (self.rightBottompPoint.y >= SCREEN_WIDTH*9/16-s) {
                return;
            }
        }
    } else if (self.videoscale == NvSSVideoCreateScaleVertical) {
        //向左滑
        if (x<0) {
            if (self.leftTopPoint.x <= s) {
                return;
            }
        } else {//向右滑
            if (self.rightBottompPoint.x >= SCREEN_WIDTH*9/16-s) {
                return;
            }
        }
        //向上滑
        if (y<0) {
            if (self.leftTopPoint.y <= s) {
                return;
            }
        } else {//向下滑
            if (self.rightBottompPoint.y >= SCREEN_WIDTH-s) {
                return;
            }
        }
    } else {
        //向左滑
        if (x<0) {
            if (self.leftTopPoint.x <= s) {
                return;
            }
        } else {//向右滑
            if (self.rightBottompPoint.x >= SCREEN_WIDTH-s) {
                return;
            }
        }
        //向上滑
        if (y<0) {
            if (self.leftTopPoint.y <= s) {
                return;
            }
        } else {//向下滑
            if (self.rightBottompPoint.y >= SCREEN_WIDTH-s) {
                return;
            }
        }
    }

    
    if (self.isDown) {
        
        CGFloat scale = sqrtf(powf(currentPoint.y - center.y, 2)+powf(currentPoint.x - center.x, 2))/sqrtf(powf(previousPoint.y - center.y, 2)+powf(previousPoint.x - center.x, 2));
        
//        if ([self.delegate respondsToSelector:@selector(rectView:rotate:scale:)]) {
//            [self.delegate rectView:self rotate:-angle*180/M_PI scale:scale];
//        }
        if ([self.delegate respondsToSelector:@selector(rectView:scale:)]) {
            [self.delegate rectView:self scale:scale];
        }
        
    } else if([self isInRect:currentPoint]) {
        if ([self.delegate respondsToSelector:@selector(rectView:currentPoint:previousPoint:)]) {
            [self.delegate rectView:self currentPoint:currentPoint previousPoint:previousPoint];
        }
    }
}

- (void)close {
    if ([self.delegate respondsToSelector:@selector(rectView:close:)]) {
        [self.delegate rectView:self close:self.leftTop];
    }
}

//重新绘制
- (void)setPoints:(NSArray *)array {
    self.leftTopPoint = [array[0] CGPointValue];
    self.leftBottomPoint = [array[1] CGPointValue];
    self.rightBottompPoint = [array[2] CGPointValue];
    self.rightTopPoint = [array[3] CGPointValue];
    self.leftTop.center = CGPointMake(self.leftTopPoint.x-10, self.leftTopPoint.y-10);
    [self setNeedsDisplay];
}

//判断点是否在四点围城rect之内
- (BOOL)isInRect:(CGPoint)p {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGMutablePathRef pathRef=CGPathCreateMutable();
    CGPathMoveToPoint(pathRef, NULL, self.leftTopPoint.x, self.leftTopPoint.y);
    CGPathAddLineToPoint(pathRef, NULL, self.leftBottomPoint.x, self.leftBottomPoint.y);
    CGPathAddLineToPoint(pathRef, NULL, self.rightBottompPoint.x, self.rightBottompPoint.y);
    CGPathAddLineToPoint(pathRef, NULL, self.rightTopPoint.x, self.rightTopPoint.y);
    CGPathCloseSubpath(pathRef);
    CGContextAddPath(ctx, pathRef);
    return CGPathContainsPoint(pathRef, nil, p, false);
}

- (CGPoint)getCenter {
    return CGPointMake((self.leftTopPoint.x+self.rightBottompPoint.x)/2, (self.leftTopPoint.y+self.rightBottompPoint.y)/2);
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(contextRef, kCGLineCapRound);
    CGContextSetLineWidth(contextRef, 2);  //线宽
    CGContextSetAllowsAntialiasing(contextRef, true);
    CGContextSetRGBStrokeColor(contextRef, 255.0 / 255.0, 0.0 / 255.0, 0.0 / 255.0, 1.0);  //线的颜色
    CGContextBeginPath(contextRef);

    CGContextMoveToPoint(contextRef, self.leftTopPoint.x-10, self.leftTopPoint.y-10);
    CGContextAddLineToPoint(contextRef, self.leftBottomPoint.x-10, self.leftBottomPoint.y+10);
    CGContextAddLineToPoint(contextRef, self.rightBottompPoint.x+10, self.rightBottompPoint.y+10);
    CGContextAddLineToPoint(contextRef, self.rightTopPoint.x+10, self.rightTopPoint.y-10);
    CGContextAddLineToPoint(contextRef, self.leftTopPoint.x-10, self.leftTopPoint.y-10);
    
    CGContextStrokePath(contextRef);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rotateRect = CGRectMake(self.rightBottompPoint.x-10, self.rightBottompPoint.y-10, 30, 30);
    CGContextAddEllipseInRect(context, rotateRect);
    [[UIColor whiteColor] set];
    CGContextFillPath(context);
    [[UIColor blackColor] set];
    [[UIImage imageFromBundle:@"rotate"] drawInRect:rotateRect];
}


@end
